package it.unibo.imola.utils;

import java.util.Collection;
import java.util.Iterator;

public class ToStringCreator {

	private StringBuilder creator = null;
	private boolean atBeginning = true;

	public ToStringCreator(Class<?> clazz) {
		creator = new StringBuilder(clazz.getSimpleName() + "{");
	}

	public ToStringCreator append(String name, String value) {
		if (!atBeginning)
			creator.append(",");
		creator.append((name == null ? "null" : name) + "=" + (value == null ? "null" : value));
		atBeginning = false;
		return this;
	}

	public ToStringCreator append(String name, Object obj) {
		return append(name, (obj == null ? null : obj.toString()));
	}

	public ToStringCreator append(String name, Object[] array) {
		if (!atBeginning)
			creator.append(",");
		creator.append(name + "=[");
		String toStringWithClassName = null;
		if (array != null) {
			if (array.length > 1) {
				toStringWithClassName = array[0].toString();
				creator.append(toStringWithClassName, array[0].getClass().getSimpleName().length(), toStringWithClassName.length());

				for (int i = 1; i < array.length; i++) {
					creator.append(",");
					toStringWithClassName = array[i].toString();
					creator.append(toStringWithClassName, array[i].getClass().getSimpleName().length(), toStringWithClassName.length());
				}
			} else if (array.length == 1) {
				toStringWithClassName = array[0].toString();
				creator.append(toStringWithClassName, array[0].getClass().getSimpleName().length(), toStringWithClassName.length());
			}
		} else
			creator.append("null");
		creator.append("]");
		atBeginning = false;
		return this;
	}
	
	public ToStringCreator append(String name, Class<?>[] array) {
		if (!atBeginning)
			creator.append(",");
		creator.append(name + "=[");
		String toStringWithClassName = null;
		if (array != null) {
			if (array.length > 1) {
				toStringWithClassName = array[0].getSimpleName();
				creator.append(toStringWithClassName);

				for (int i = 1; i < array.length; i++) {
					creator.append(",");
					toStringWithClassName = array[i].toString();
					creator.append(toStringWithClassName);
				}
			} else if (array.length == 1) {
				toStringWithClassName = array[0].toString();
				creator.append(toStringWithClassName);
			}
		} else
			creator.append("null");
		creator.append("]");
		atBeginning = false;
		return this;
	}

	public ToStringCreator append(String name, Collection<?> collection) {
		if (!atBeginning)
			creator.append(",");
		creator.append(name + "=<");
		String toStringWithClassName = null;
		Object nextObject = null;
		if (collection != null) {
			if (collection.size() > 1) {
				Iterator<?> i = collection.iterator();
				nextObject = i.next();
				toStringWithClassName = nextObject.toString();
				creator.append(toStringWithClassName, nextObject.getClass().getSimpleName().length(), toStringWithClassName.length());
				while (i.hasNext()) {
					creator.append(",");
					nextObject = i.next();
					toStringWithClassName = nextObject.toString();
					creator.append(toStringWithClassName, nextObject.getClass().getSimpleName().length(), toStringWithClassName.length());
				}
			} else if (collection.size() == 1) {
				nextObject = collection.iterator().next();
				toStringWithClassName = nextObject.toString();
				creator.append(toStringWithClassName, nextObject.getClass().getSimpleName().length(), toStringWithClassName.length());
			}
		} else
			creator.append("null");
		creator.append(">");
		atBeginning = false;
		return this;

	}

	@Override
	public String toString() {
		return creator.append("}").toString();
	}
}
